# customer/adminx.py

import xadmin

from .models import Company,Contacter,RigData,RigWork
from django.utils.safestring import mark_safe 
from xadmin import views

class CompanyAdmin(object):
    list_display = ['id', 'fullname', 'companytype','uscc','ceo','ceoid','bankname','accountnumber','address','enable','desc']
    search_fields = ['id', 'fullname', 'companytype','uscc','ceo','ceoid','bankname','accountnumber','address','enable','desc']
    list_filter = ['id', 'fullname', 'companytype','uscc','ceo','ceoid','bankname','accountnumber','address','enable','desc']
    # def save_models(self):
    #     self.new_obj.operator = self.request.user
    #     super().save_models()
xadmin.site.register(Company,CompanyAdmin)

class ContacterAdmin(object):
    list_display = ['company', 'name', 'tel1','tel2','tel3','position','enable','desc']
    search_fields = ['company', 'name', 'tel1','tel2','tel3','position','enable','desc']
    list_filter = ['company', 'name', 'tel1','tel2','tel3','position','enable','desc']
    exclude = ['add_by']
    def save_models(self):
        self.new_obj.add_by = self.request.user
        super().save_models()
    def save_models(self):
        self.new_obj.add_by = self.request.user
        # self.new_obj.save()
        super().save_models()

    def get_readonly_fields(self):
        """  重新定义此函数，限制普通用户所能修改的字段  """
        if self.user.is_superuser:
            self.readonly_fields = []
        return self.readonly_fields
    readonly_fields = ('add_by',)
xadmin.site.register(Contacter,ContacterAdmin)

class RigDataAdmin(object):

    list_display = ['name', 'city', 'same_city_boss','get_worknum']
    search_fields = ['name', 'desc']
    list_filter = ['city', 'praise','isactive']
    exclude=['get_worknum','same_city_boss','add_by']#隐藏字段
    def save_models(self):
        self.new_obj.add_by = self.request.user
        super().save_models()

    def get_readonly_fields(self):
        """  重新定义此函数，限制普通用户所能修改的字段  """
        if self.user.is_superuser:
            #self.readonly_fields = ['get_worknum']
            # self.exclude=['get_worknum']#隐藏字段
            pass
        else:
            self.readonly_fields=['add_by']

        return self.readonly_fields
    def save_models(self):
        self.new_obj.add_by = self.request.user
        super().save_models()

xadmin.site.register(RigData,RigDataAdmin)

class RigWorkAdmin(object):
    list_display = ['project', 'rigboss', 'holes','depths','price']
    search_fields = ['project', 'rigboss','price']
    list_filter = ['project', 'rigboss', 'price']
    exclude = ['add_by']
    def save_models(self):
        self.new_obj.add_by = self.request.user
        super().save_models()
    def get_readonly_fields(self):
        """  重新定义此函数，限制普通用户所能修改的字段  """
        if self.user.is_superuser:
            self.readonly_fields = []
        else:
            self.readonly_fields=['add_by']
        return self.readonly_fields
xadmin.site.register(RigWork,RigWorkAdmin)